package ua.shop;


import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;

public class Parser {
    private static Logger logger = Logger.getLogger(Parser.class);

    private Connector connector = new Connector();
    private Elements elements;
    private Document document = new Document("");
    private BeanRia currentBeanRia = new BeanRia();
    private ArrayList<BeanRia> beanRias = new ArrayList<BeanRia>();
    private Element currentElement;


    public Parser(String url) throws ConnectionException {
        connector.connect(url);
        document = connector.getDocument();
        elements = new Elements();
        elements = getElements(RiaSelect.FLATS);
    }

    public void parse() {
        currentElement = elements.first();
        for (Element el : elements) {
            formBeanRia();

            currentElement = el;

            beanRias.add(currentBeanRia);

            logger.info("parse testing --> " + el.equals(currentElement));
        }
    }

    private void formBeanRia() {

        getPrize(RiaSelect.PRIZE);
        getReference(RiaSelect.A, RiaSelect.HREF);
        getRooms(RiaSelect.ITEM_CHAR);
        getSquare(RiaSelect.ITEM_CHAR);
        getFloor(RiaSelect.ITEM_CHAR);
        getDescription(RiaSelect.DESCRIPTION);
        getDataPost(RiaSelect.DATA_POST);
        getAddress(RiaSelect.ADDRESS);
    }

    //TODO where tests
    private Elements getElements(String elem) {
        //TODO delete loggers
        logger.debug("getDocument() document has text? = " + document.hasText());

        elements = document.getElementsByClass(elem);
        logger.debug("getDocument() elements has text? " + elements.hasText());
        logger.debug("count of elements = " + elements.size());

        logger.debug("first point = " + elements.first().text());

        return elements;
    }

    //TODO where tests
    private void getPrize(String prize) {

        String str = currentElement.getElementsByClass(prize).first().text();
        currentBeanRia.setPrize(str);
        logger.debug("prize = " + str);
    }

    //TODO where tests
    private void getSquare(String itemChar) {
        String str = currentElement.getElementsByClass(itemChar).get(1).text();
        currentBeanRia.setSquare(str);
        logger.debug("square = " + str);
    }

    //TODO where tests
    private void getReference(String a, String href) {
        String str = currentElement.select(a).first().attr(href);
        currentBeanRia.setReference(str);
        logger.debug("href = " + str);
    }

    //TODO where tests
    private void getRooms(String itemChar) {
        String str = currentElement.getElementsByClass(itemChar).first().text();
        currentBeanRia.setRooms(str);
        logger.debug("rooms = " + str);
    }

    //TODO where tests
    private void getFloor(String itemChar) {
        String str = currentElement.getElementsByClass(itemChar).last().text();
        currentBeanRia.setFloor(str);
        logger.debug("floor = " + str);
    }

    //TODO where tests
    private void getDescription(String description) {
        String str = currentElement.getElementsByClass(description).text();
        currentBeanRia.setDescription(str);
        logger.debug("description = " + str);
    }

    //TODO where tests
    private void getDataPost(String dataPost) {
        String str = currentElement.getElementsByClass(dataPost).text();
        currentBeanRia.setDatapost(str);
        logger.debug("data post = " + str);
    }

    //TODO where tests
    private void getAddress(String address) {
        String str = currentElement.getElementsByClass(address).last().text();
        currentBeanRia.setAddress(str);
        logger.debug("address = " + str);

    }


    public BeanRia getCurrentBeanRia(int item) {
        return beanRias.get(item);
    }

    public String toString() {
        StringBuilder str = new StringBuilder();
        str.append("{");
        for (BeanRia bin : beanRias) {
            str.append("{");
            str.append(bin.toString());
            str.append("} ");
            str.append("\n");
        }
        str.append("}");
        return str.toString();
    }
}
